package br.com.lavitstudio.game;

import java.awt.Color;
import java.awt.Graphics;

public class Sky {
	public static int day = 0;
	public static int night = 1;
	public static int time = day;
	
	public int r1 = 70, g1 = 120, b1 = 230; // Day
	public int r2 = 15, g2 = 15, b2 = 80; // Night
	public int r = r1, g = g1, b = b1;
	public int dayFrame = 0;
	public int dayTime = 60000; // 2000/5, 5000/12, 10000/26, 20000/54, 40000/110, 60000/165
	public int sun_moonTime = 165;
	public int changeFrame = 0;
	public int changeTime = 4;
	public int sun_moonMove = 0;
	public int animation = 0;
	
	public boolean isSun = true;
	public boolean isMoon = false;
	
	public Sky() {
		if(time == day){
			r = r1;
			g = g1;
			b = b1;
		} else if(time == night){
			r = r2;
			g = g2;
			b = b2;
		}
	}
	
	
	
	public void tick() {
		if(dayFrame >= dayTime) {
			if(time == day){
				time = night;
				isMoon = true;
				isSun = false;
				Mob.killAllMobsNight();
			} else if(time == night) {
				time = day;
				isMoon = false;
				isSun = true;
				Mob.killAllMobsDay();
			}
			sun_moonMove  = 0;
			dayFrame = 0;
		} else {
			dayFrame += 1;
			sun_moonMove = dayFrame / sun_moonTime;
		}
		
		if(changeFrame >= changeTime) {
			if(time == day) {
				if (r < r1){
					r += 1;
				}
				if (g < g1){
					g += 1;
				}
				if (b < b1){
					b += 1;
				}
			} else if (time == night) {
				if (r > r2){
					r -= 1;
				}
				if (g > g2){
					g -= 1;
				}
				if (b > b2){
					b -= 1;
				}
			}
			changeFrame = 0;
		} else {
			changeFrame += 1;
		}
	}
	
	public void render(Graphics gr) {
		gr.setColor(new Color(r, g ,b));
		gr.fillRect(0, 0, Component.pixel.width, Component.pixel.height);
				
		if(isSun){
			gr.drawImage(
					Tile.tile_sun, // img
					sun_moonMove - Tile.tileSize, // dx1
					Tile.tileSize / 2, // dy1
					Tile.tileSize + (sun_moonMove - Tile.tileSize), // dx2
					(Tile.tileSize * 2) + (Tile.tileSize / 2), // dy2
					
					Tile.sun[0] * Tile.tileSize, // sx1
					Tile.sun[0] * Tile.tileSize, // sy1
					Tile.sun[1] + Tile.tileSize, // sx2
					Tile.sun[1] + (Tile.tileSize * 2), // sy2
					null);
		}
		
		if(isMoon){
			gr.drawImage(
					Tile.tile_moon, // img
					sun_moonMove - Tile.tileSize, // dx1
					Tile.tileSize / 2, // dy1
					Tile.tileSize + (sun_moonMove - Tile.tileSize), // dx2
					(Tile.tileSize * 2) + (Tile.tileSize / 2), // dy2
					
					Tile.moon[0] * Tile.tileSize, // sx1
					Tile.moon[0] * Tile.tileSize, // sy1
					Tile.moon[1] + Tile.tileSize, // sx2
					Tile.moon[1] + (Tile.tileSize * 2), // sy2
					null);
		}

	}
}
